package org.akomantoso.core.data;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.akomantoso.core.data.attribute.Enactment;
import org.akomantoso.core.data.attribute.IdReq;
import org.akomantoso.core.data.attribute.Modifiers;
import org.akomantoso.core.data.attribute.Refers;
import org.akomantoso.core.data.type.StatusType;
import org.akomantoso.core.io.Externalizable;
import org.akomantoso.core.io.Input;
import org.akomantoso.core.io.InputUtil;
import org.akomantoso.core.io.Output;
import org.akomantoso.core.io.OutputUtil;
import org.akomantoso.core.io.util.ModificationTypeElementUtil;

/**
 * 
 * The complex type modificationType lists all the properties associated to modification elements.
 * 
 * <pre>
 * 	<xsd:complexType name="modificationType">
 * 		<xsd:sequence>
 * 			<xsd:element ref="source" minOccurs="1" maxOccurs="unbounded" />
 * 			<xsd:element ref="destination" minOccurs="1" maxOccurs="unbounded" />
 * 			<xsd:element ref="force" minOccurs="0" maxOccurs="1" />
 * 			<xsd:element ref="efficacy" minOccurs="0" maxOccurs="1" />
 * 			<xsd:element ref="application" minOccurs="0" maxOccurs="1" />
 * 			<xsd:element ref="duration" minOccurs="0" maxOccurs="1" />
 * 			<xsd:element ref="condition" minOccurs="0" maxOccurs="1" />
 * 		</xsd:sequence>
 * 		<xsd:attributeGroup ref="idreq" />
 * 		<xsd:attributeGroup ref="enactment" />
 * 		<xsd:attributeGroup ref="modifiers" />
 * 		<xsd:attributeGroup ref="refers" />
 * 	</xsd:complexType>
 * </pre>
 * 
 * @author <a href="mailto:jacques.militello@gmail.com">Jacques Militello</a>
 * 
 * @since http://www.akomantoso.org/1.0
 * 
 */
public abstract class ModificationType implements IdReq, Enactment, Refers, Modifiers, Externalizable {

	// IdReq
	private String _id;
	private String _evolvingId;

	// enactment
	private String _period;
	private StatusType _statusType;

	// Refers
	private String _refersTo;

	// Modifiers
	private Boolean _exclusion;
	private Boolean _incomplete;

	private List<ModificationTypeElement> _elements;

	/**
	 * @see org.akomantoso.core.data.attribute.IdReq#getId()
	 */
	public String getId() {
		return _id;
	}

	/**
	 * @see org.akomantoso.core.data.attribute.IdReq#setId(java.lang.String)
	 */
	public void setId(String id) {
		_id = id;
	}

	/**
	 * @see org.akomantoso.core.data.attribute.IdReq#getEvolvingId()
	 */
	public String getEvolvingId() {
		return _evolvingId;
	}

	/**
	 * @see org.akomantoso.core.data.attribute.IdReq#setEvolvingId(java.lang.String)
	 */
	public void setEvolvingId(String evolvingId) {
		_evolvingId = evolvingId;
	}

	/**
	 * @see org.akomantoso.core.data.attribute.Period#getPeriod()
	 */
	public String getPeriod() {
		return _period;
	}

	/**
	 * @see org.akomantoso.core.data.attribute.Period#setPeriod(java.lang.String)
	 */
	public void setPeriod(String period) {
		_period = period;
	}

	/**
	 * @see org.akomantoso.core.data.attribute.Refers#getRefersTo()
	 */
	public String getRefersTo() {
		return _refersTo;
	}

	/**
	 * @see org.akomantoso.core.data.attribute.Refers#setRefersTo(java.lang.String)
	 */
	public void setRefersTo(String refersTo) {
		_refersTo = refersTo;
	}

	/**
	 * @see org.akomantoso.core.data.attribute.Enactment#getStatus()
	 */
	public StatusType getStatus() {
		return _statusType;
	}

	/**
	 * @see org.akomantoso.core.data.attribute.Enactment#setStatus(org.akomantoso.core.data.type.StatusType)
	 */
	public void setStatus(StatusType status) {
		_statusType = status;
	}

	/**
	 * @see org.akomantoso.core.data.attribute.Modifiers#getExclusion()
	 */
	public Boolean getExclusion() {
		return _exclusion;
	}

	/**
	 * @see org.akomantoso.core.data.attribute.Modifiers#setExclusion(java.lang.Boolean)
	 */
	public void setExclusion(Boolean exclusion) {
		_exclusion = exclusion;
	}

	/**
	 * @see org.akomantoso.core.data.attribute.Modifiers#getIncomplete()
	 */
	public Boolean getIncomplete() {
		return _incomplete;
	}

	/**
	 * @see org.akomantoso.core.data.attribute.Modifiers#setIncomplete(java.lang.Boolean)
	 */
	public void setIncomplete(Boolean incomplete) {
		_incomplete = incomplete;
	}

	public List<ModificationTypeElement> getElements() {
		return _elements;
	}

	public void setElements(List<ModificationTypeElement> elements) {
		_elements = elements;
	}

	/**
	 * @see org.akomantoso.core.io.Externalizable#writeExternal(org.akomantoso.core.io.Output)
	 */
	public <T extends Output> void writeExternal(T out) throws IOException {
		OutputUtil.writeIdReq(out, this);
		OutputUtil.writeEnactment(out, this);
		OutputUtil.writeRefers(out, this);
		OutputUtil.writeModifiers(out, this);
		if (_elements != null) {
			for (int i = 0, n = _elements.size(); i < n; i++) {
				_elements.get(i).writeExternal(out);
			}
		}

	}

	/**
	 * @see org.akomantoso.core.io.Externalizable#readExternal(org.akomantoso.core.io.Input)
	 */
	public <T extends Input> void readExternal(T in) throws IOException {

		_id = in.readAttribute(IdReq.ATTRIBUTE_ID);
		_evolvingId = in.readAttribute(IdReq.ATTRIBUTE_EVOLVING_ID);

		// enactment
		_period = in.readAttribute(Enactment.ATTRIBUTE);
		_statusType = StatusType.checkValueOf(in.readAttribute(Enactment.ATTRIBUTE_STATUS));

		// Refers
		_refersTo = in.readAttribute(Refers.ATTRIBUTE);

		// Modifiers
		_exclusion = InputUtil.readBoolean(in.readAttribute(Modifiers.ATTRIBUTE_EXCLUSION));
		_incomplete = InputUtil.readBoolean(in.readAttribute(Modifiers.ATTRIBUTE_INCOMPLETE));

		ModificationTypeElementUtil.read(in, _elements = new ArrayList<ModificationTypeElement>());
	}

}
